package com.wo2record.blog.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.wo2record.base.action.BaseAction;
import com.wo2record.blog.model.Article;
import com.wo2record.blog.service.ArticleService;
import com.wo2record.user.model.UserLogin;
import com.wo2record.user.service.UserLoginService;

public class ArticleAction  extends BaseAction{

	private static final long serialVersionUID = 1075429931733341798L;

	@Autowired
	private ArticleService articleService;
	@Autowired
	private UserLoginService userLoginService;
	
	private Article article;
	private List<Article> articles;
	private String articleId;
	
	public String list() throws Exception{
		articles = articleService.getObjects();
		return ActionSupport.SUCCESS;
	}
	
	public String save() throws Exception{
		try{
			// TODO since we do not have user creating function, we use this kind of 
			// thing to make a test.
			String userLoginId = "40286e813220b545013220b7045a0001";
			UserLogin user = userLoginService.getObject(userLoginId);
			article.setUserLogin(user);
			articleService.saveObject(article);
			return ActionSupport.SUCCESS;
		}catch(Exception e){
			throw e;
		}
	}
	
	public String view() throws Exception{
		try{
			article = articleService.getObject(articleId);
			return ActionSupport.SUCCESS;
		}catch(Exception e){
			throw e;
		}
	}

	// Getter and Setter
	public Article getArticle() {
		return article;
	}
	public void setArticle(Article article) {
		this.article = article;
	}
	public List<Article> getArticles() {
		return articles;
	}
	public void setArticles(List<Article> articles) {
		this.articles = articles;
	}
	public String getArticleId() {
		return articleId;
	}
	public void setArticleId(String articleId) {
		this.articleId = articleId;
	}
}
